""""
Case Type   : D库常用语法兼容-常用函数兼容
Case Name   : 创建内存表执行修改操作查看sys.tables视图返回值测试
Create At   : 2025-10-10
Owner       : chen-czywj
Description :
    1.使用兼容D库创建数据库
    2.执行创建系统表测试
    3.查看视图返回值正确无误
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.查看视图返回值正确无误
    4.清理环境
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from yat.test import Node
from yat.test import macro

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Function_Commonviews_Case0023(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.com = Common()
        self.pri_node = Node('PrimaryDbUser')
        self.dbname_d = Node('PrimaryDbDmode').db_name
        self.dbname = 'test_views_001'
        self.schema_name = 'sys_view_test_01'
        self.tb_name = 'tb_commonviews_023'
        self.incre_checkpoint = self.com.show_param('enable_incremental_checkpoint')
        self.mot_server = self.com.show_param('enable_mot_server')

    def test_Function_Commonviews(self):
        text = '-------------step1: 使用兼容D库创建数据库; expect: 成功-------------'
        log.info(text)
        set_param = primary_sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_incremental_checkpoint = off")
        log.info(set_param)
        self.assertTrue(set_param, '参数修改异常')
        set_param = primary_sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_mot_server = on")
        log.info(set_param)
        self.assertTrue(set_param, '参数修改异常')
        log.info('---重启集群---')
        res = primary_sh.restart_db_cluster()
        log.info(res)
        self.assertTrue(res)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_db = f'''drop database if exists {self.dbname};
            create database if not exists {self.dbname} dbcompatibility = 'D';'''
        res = primary_sh.execut_db_sql(create_db, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res)
        load_extension = primary_sh.execut_db_sql('create extension shark;',
                                     dbname=self.dbname)
        log.info(load_extension)
        self.assertIn(self.constant.create_extension_success, load_extension)

        text = '-------------step2: 执行创建系统表测试; expect: 成功-------------'
        log.info(text)
        create_tb = f'''set search_path to sys;
            create FOREIGN table {self.tb_name}(x int);'''
        res = primary_sh.execut_db_sql(create_tb, dbname=self.dbname)
        log.info(res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, res)
        self.assertIn("permission denied to create", res)
        create_tb = f'''create schema {self.schema_name};
                set search_path to {self.schema_name};
            create FOREIGN table {self.tb_name}(x int);'''
        res = primary_sh.execut_db_sql(create_tb, dbname=self.dbname)
        log.info(res)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, res)

        text = '-------------step3: 查看视图返回值正确无误; expect: 返回值正确-------------'
        log.info(text)
        select_sql = f"""select name, object_id,principal_id,schema_id,type,create_date,
            modify_date,temporal_type_desc from sys.tables ;"""
        res = primary_sh.execut_db_sql(select_sql, dbname=self.dbname)
        log.info(res)
        self.assertIn(self.tb_name, res)

    def tearDown(self):
        text = '------------------step4: 清理环境; expect: 成功------------------'
        log.info(text)
        set_param = primary_sh.execute_gsguc('set',
                    self.constant.GSGUC_SUCCESS_MSG,
                    f"enable_incremental_checkpoint = {self.incre_checkpoint}")
        log.info(set_param)
        self.assertTrue(set_param, '参数修改异常')
        set_param = primary_sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_mot_server = {self.mot_server}")
        log.info(set_param)
        self.assertTrue(set_param, '参数修改异常')
        drop_db = f'''set search_path to {self.schema_name};
            drop FOREIGN table if exists {self.tb_name};'''
        res = primary_sh.execut_db_sql(drop_db, dbname=self.dbname)
        log.info(res)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res)
        drop_db = f'''drop database if exists {self.dbname};'''
        res = primary_sh.execut_db_sql(drop_db, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res)
        num = self.com.get_node_num(self.pri_node)
        log.info(num)
        if int(num) > 1:
            res = primary_sh.check_location_consistency('primary', num, 300)
            self.assertTrue(res, '主备同步失败')
        log.info('---重启集群---')
        res = primary_sh.restart_db_cluster()
        log.info(res)
        self.assertTrue(res)
